import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_model/user/user_income_info_model.dart';
import 'package:xvideos/hj_page/mine_business/make_money/withdrawal_page.dart';
import 'package:xvideos/hj_page/mine_business/welfare/share_details_page.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';
import 'package:xvideos/hj_utils/api_service/mine_service.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';

class IncomeData {
  String value;
  String title;

  IncomeData(this.title, this.value);
}

class ShareDataListPage extends StatefulWidget {
  const ShareDataListPage({super.key});

  @override
  State<ShareDataListPage> createState() => _ShareDataListPageState();
}

class _ShareDataListPageState extends State<ShareDataListPage> {
  UserIncomeModel? _model;
  bool isLoading = true;
  final incomeList = <IncomeData>[];
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      final res = await MineService.fetchAllIncomeInfo();
      isLoading = false;
      _model = res;
      incomeList.add(IncomeData("当月收益（元）", _model?.monthIncomeAmount ?? '0'));
      incomeList.add(IncomeData("当月推广数", _model?.monthInviteUserCount ?? '0'));
      incomeList.add(IncomeData("今日收益（元）", _model?.todayIncomeAmount ?? '0'));
      incomeList.add(IncomeData("今日推广数", _model?.todayInviteUserCount ?? '0'));
      if (mounted) setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('推广数据'),
          actions: [
            GestureDetector(
              onTap: () => Get.to(ShareDetailsPage(), arguments: _model),
              child: Text(
                '收益明细',
                style: TextStyle(color: Color(0xFF989898), fontSize: 12),
              ),
            ),
            16.sizeBoxW
          ],
        ),
        body: () {
          if (isLoading) return LoadingCenterWidget();
          if (_model == null) return CErrorWidget();
          return Padding(
            padding: EdgeInsets.only(left: 12, right: 12, top: 12),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  height: 160,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(8),
                      border: Border.all(color: const Color(0xff1e262e), width: 1),
                      color: Color(0xFF131b23)
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Expanded(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            Expanded(
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Text(
                                    "钱包余额",
                                    style: const TextStyle(color: const Color(0xffb8bbbd), fontWeight: FontWeight.w900, fontSize: 12.0),
                                  ),
                                  Text(
                                    "${_model?.totalAmount}",
                                    style: const TextStyle(color: const Color(0xffffffff), fontWeight: FontWeight.w500, fontSize: 24.0),
                                  )
                                ],
                              ),
                            ),
                            SizedBox(
                              height: 57,
                              width: .5,
                              child: DecoratedBox(decoration: BoxDecoration(color: Color(0xff444444))),
                            ),
                            Expanded(
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Text(
                                    "累积收益",
                                    style: const TextStyle(color: const Color(0xffb8bbbd), fontWeight: FontWeight.w900, fontSize: 12.0),
                                  ),
                                  Text(
                                    "${_model?.totalIncomeAmount}",
                                    style: const TextStyle(color: const Color(0xffffffff), fontWeight: FontWeight.w500, fontSize: 24.0),
                                  )
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                      InkWell(
                        onTap: () => Get.to(WithdrawalPage()),
                        child: Container(
                          width: 276,
                          height: 36,
                          margin: EdgeInsets.symmetric(vertical: 15),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(18)),
                            color: Color(0xFFcf452f),
                          ),
                          child: Center(
                            child: Text(
                              "立即提现",
                              style: const TextStyle(color: const Color(0xffffffff), fontWeight: FontWeight.w900, fontSize: 12.0),
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                12.sizeBoxH,
                Container(
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(6)),
                      border: Border.all(color: const Color(0xff1e262e), width: 1),
                      color: Color(0xFF131b23),
                  ),
                  child: GridView.count(
                    shrinkWrap: true,
                    physics: NeverScrollableScrollPhysics(),
                    crossAxisCount: 2,
                    childAspectRatio: 5 / 3,
                    children: incomeList.map((e) => _buildIncomeGItem(e)).toList(),
                  ),
                ),
                15.sizeBoxH,
                Text(
                  "推广总统计",
                  style: const TextStyle(color: const Color(0xffffffff), fontWeight: FontWeight.w600, fontSize: 16.0),
                ),
                15.sizeBoxH,
                _buildTGItem("累计推广用户", _model?.totalInviteUserCount ?? '0'),
                _buildTGItem("累计付费用户", _model?.totalPayUserCount ?? '0'),
              ],
            ),
          );
        }());
  }

  Widget _buildTGItem(String title, String value) {
    return Container(
      height: 35,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            title,
            style: const TextStyle(color: const Color(0xffb8bbbd), fontWeight: FontWeight.w400, fontSize: 12.0),
          ),
          Text(
            "$value",
            style: const TextStyle(color: const Color(0xffffffff), fontWeight: FontWeight.w500, fontSize: 18.0),
          ),
        ],
      ),
    );
  }

  Widget _buildIncomeGItem(IncomeData item) {
    return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            "${item.value}",
            style: const TextStyle(color: const Color(0xffffffff), fontWeight: FontWeight.w900, fontSize: 18.0),
          ),
          Text(
            item.title,
            style: const TextStyle(color: const Color(0xffb8bbbd), fontWeight: FontWeight.w400, fontSize: 12.0),
          )
        ],
      ),
    );
  }
}
